package com.sy.msgview

import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Database
import androidx.room.Entity
import androidx.room.Query
import androidx.room.RoomDatabase
import androidx.room.ForeignKey
import kotlinx.coroutines.flow.Flow

@Entity(tableName = "qq_list", primaryKeys = ["qq_id"])
data class QQItem(val qq_id: Int, val qq: Long)

@Entity(tableName = "msg_content", primaryKeys = ["msg_id"])
data class QQMsgItem(
	val msg_id: Long,
	val send_qq: Long,
	val to_qq: Long,
	val send_name: String?,
	val msg: String?,
	val time: Long?,
	val date: Long?,
	@ColumnInfo(defaultValue = "0")
	val is_me: Int
)

@Dao
interface QQListDao {
	@Query("SELECT qq_list.qq_id, msg_content.to_qq as qq FROM msg_content JOIN qq_list ON msg_content.to_qq = qq_list.qq GROUP BY to_qq ORDER BY COUNT(to_qq) DESC")
	fun getAllQQ(): Flow<MutableList<QQItem>>
}

@Dao
interface MsgItemDao {
	@Query("SELECT * FROM msg_content WHERE send_qq = :qq OR to_qq = :qq ORDER BY time DESC LIMIT :count OFFSET :offset")
	fun getMsgItem(qq: Long, count: Int = 15, offset: Int = 0): Flow<MutableList<QQMsgItem>>
}

@Database(entities = [QQItem::class, QQMsgItem::class], version = 1)
abstract class QQDatabase : RoomDatabase() {
	abstract fun qqListDao(): QQListDao
	abstract fun msgItemDao(): MsgItemDao
}

enum class Screen(val route: String) {
	MAIN("main"),
	MSG("msg")
}
